package com.mobilekites.adapters;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.toolbox.ImageLoader;
import com.mobilekites.api.model.BucketedList;

public abstract class CardListAdapter<T> extends KitesListAdapter<T> {
	private static final int LOADING_TYPE = 1;
	private static final int ERROR_TYPE = 0;
	private static final int TYPE_TOTAL = 2;

	protected final ImageLoader mImageLoader;

	public CardListAdapter(Context context, BucketedList<T> bucketedList,
			ImageLoader imageLoader) {
		super(context, bucketedList);
		mImageLoader = imageLoader;
	}

	@Override
	public int getCount() {
		int i = mBucketedList.getCount();
		if (getFooterMode() != PaginatedListAdapter.FooterMode.NONE)
			i++;
		return i;
	}

	@Override
	public Object getItem(int position) {
		return mBucketedList.getItem(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		int type = getCardListAdapterViewType(position);
		if (type == ERROR_TYPE) {
			return getErrorFooterView(convertView, parent);
		} else if (type == LOADING_TYPE) {
			return getLoadingFooterView(convertView, parent);
		} else {
			return getViewByData(position, convertView, parent);
		}
	}

	protected abstract View getViewByData(int position, View convertView,
			ViewGroup parent);

	private int getCardListAdapterViewType(int position) {
		if (getFooterMode() != FooterMode.NONE && position == getCount() - 1) {
			if (getFooterMode() == FooterMode.LOADING) {
				return LOADING_TYPE;
			} else {
				return ERROR_TYPE;
			}
		} else {
			return getItemAdapterViewType(position);
		}
	}

	protected abstract int getItemAdapterViewType(int position);

	@Override
	public int getItemViewType(int position) {
		return getCardListAdapterViewType(position);
	}

	@Override
	public int getViewTypeCount() {
		return TYPE_TOTAL;
	}

	@Override
	public void onDataChanged() {
		super.onDataChanged();
	}

	public void updateAdapterData(BucketedList<T> bucketedlist) {
		super.updateAdapterData(bucketedlist);
		notifyDataSetChanged();
	}

	@Override
	public void onRestoreInstanceState(ListView listview, Bundle bundle) {
		// ArrayList arraylist = bundle.getParcelableArrayList("data");
		// if (arraylist != null && arraylist.size() != 0) {
		// int j = bundle.getInt(KEY_FIRSTVISIBLE_ROW);
		// int i = bundle.getInt(KEY_ROW_PIXEL_OFFSET);
		// notifyDataSetChanged();
		// listview.setSelectionFromTop(j, i);
		// } else {
		// notifyDataSetChanged();
		// mBucketedList.startLoadItems();
		// super.onRestoreInstanceState(listview, bundle);
		// }
	}

	@Override
	public void onSaveInstanceState(ListView listview, Bundle bundle) {
		super.onSaveInstanceState(listview, bundle);
		// if (mBucketedList.getCount() != 0) {
		// int j = listview.getFirstVisiblePosition();
		// int i = 0;
		// if (j >= mBucketedList.getCount() || listview.getChildCount() <= 0)
		// j = -1 + mBucketedList.getCount();
		// else
		// i = listview.getChildAt(0).getTop();
		// bundle.putInt(KEY_FIRSTVISIBLE_ROW, j);
		// bundle.putInt(KEY_ROW_PIXEL_OFFSET, i);
		// }
	}

	@Override
	public void onMovedToScrapHeap(View arg0) {
	}
}
